/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pio.action.aspirante;

import DBMS.DataBaseManager;
import clases.*;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author andreth
 */
public class VocacionalAction extends DispatchAction {

    private static final String POPULATE = "populate";
    private static final String FAILURE = "failure";
    private static final String SUCCESS = "success";

    public ActionForward populate(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DataBaseManager dbms = DataBaseManager.getInstance();
//        Test t = new Test();
        ArrayList<Pregunta> preguntas = dbms.consultarPreguntas();
        // PARA QUE NO SE SELECCIONEN LAS OPCIONES CORRECTAS
        for (Pregunta p : preguntas) {
            for (Opcion o : p.getOpciones()) {
                o.setCorrecta(false);
            }
        }
        ((Vocacional)form).setPreguntas(preguntas);
        ((Vocacional)form).setCedula((String) 
                request.getSession().getAttribute("cedula"));
//        t.setPreguntas(preguntas);
//        request.setAttribute("Test", t);
        System.out.println("Enlazando a contestar un test vocacional");
        return mapping.findForward(POPULATE);
    }

    //Sucess si completo el test completo failure eoc 
    public ActionForward save(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Vocacional v = (Vocacional) form;
        System.out.println("El vocacional recibido es: ");
        System.out.println(v);
        if (v.getCedula() == null || v.getCedula().isEmpty()) {
            v.setCedula((String) request.getSession().getAttribute("cedula"));
        }
        System.out.println("valides " + v.valido());
        if (v.valido()) {
            System.out.println("Vocacional valido");
            DataBaseManager dbms = DataBaseManager.getInstance();
            if (dbms.agregarVocacional(v)) {
                Aspirante a = new Aspirante();
                a.setCedula(v.getCedula());
                a.setDatosCompletos(true);
                dbms.modificarAspirante(a);
                request.getSession().invalidate();
                Usuario u = new Usuario();
                u.setCedula(String.valueOf(v.getCedula()));
                List list = dbms.consultarUsuario(u);
                if (!list.isEmpty()){
                    u = (Usuario) list.get(0);
                }
                list = dbms.consultarPeriodo(new Periodo());
                request.setAttribute("Periodo", list.get(0));
                request.setAttribute("Usuario", u);
                v.setMensaje("");
                return mapping.findForward(SUCCESS);
            }
        }
        
        v.setMensaje("Por favor seleccione el numero de respuestas indicado en la pregunta");
        return this.populate(mapping, form, request, response);
    }
}
